package com.agriculture.competition.controller;

import com.agriculture.common.core.utils.poi.ExcelUtil;
import com.agriculture.common.core.web.controller.BaseController;
import com.agriculture.common.core.web.domain.AjaxResult;
import com.agriculture.common.core.web.page.TableDataInfo;
import com.agriculture.common.log.annotation.Log;
import com.agriculture.common.log.enums.BusinessType;
import com.agriculture.competition.domain.JaClassify;
import com.agriculture.competition.service.IJaClassifyService;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 分类管理Controller
 *
 * @author agriculture
 * @date 2024-11-13
 */
@RestController
@RequestMapping("/classify")
public class JaClassifyController extends BaseController {

    @Autowired
    private IJaClassifyService jaClassifyService;

    /**
     * 查询分类管理列表
     */
    // @RequiresPermissions("competition:classify:list")
    @GetMapping("/list")
    public TableDataInfo list(JaClassify jaClassify) {
        startPage();
        List<JaClassify> list = jaClassifyService.selectJaClassifyList(jaClassify);
        return getDataTable(list);
    }

    /**
     * 导出分类管理列表
     */
    // @RequiresPermissions("competition:classify:export")
    @Log(title = "分类管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JaClassify jaClassify) {
        List<JaClassify> list = jaClassifyService.selectJaClassifyList(jaClassify);
        ExcelUtil<JaClassify> util = new ExcelUtil<JaClassify>(JaClassify.class);
        util.exportExcel(response, list, "分类管理数据");
    }

    /**
     * 获取分类管理详细信息
     */
    // @RequiresPermissions("competition:classify:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(jaClassifyService.selectJaClassifyById(id));
    }

    /**
     * 新增分类管理
     */
    // @RequiresPermissions("competition:classify:add")
    @Log(title = "分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JaClassify jaClassify) {
        return toAjax(jaClassifyService.insertJaClassify(jaClassify));
    }

    /**
     * 修改分类管理
     */
    // @RequiresPermissions("competition:classify:edit")
    @Log(title = "分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JaClassify jaClassify) {
        return toAjax(jaClassifyService.updateJaClassify(jaClassify));
    }

    /**
     * 删除分类管理
     */
    // @RequiresPermissions("competition:classify:remove")
    @Log(title = "分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(jaClassifyService.deleteJaClassifyByIds(ids));
    }


    /**
     * 查询所有一级菜单
     *
     * @param jaClassify
     * @return
     */
    // @RequiresPermissions("competition:classify:queryParentClassify")
    @PostMapping("/queryParentClassify")
    public AjaxResult queryParentClassify(@RequestBody JaClassify jaClassify) {
        jaClassify.setLeval(0L);
        List<JaClassify> list = jaClassifyService.selectJaClassifyList(jaClassify);
        return success(list);
    }

    /**
     * 根据父id查子类
     *
     * @param id
     * @return
     */
    // @RequiresPermissions("competition:classify:queryClassify")
    @GetMapping("/queryClassify")
    public AjaxResult queryClassify(@RequestParam("id") Long id) {
        JaClassify jaClassify = new JaClassify();
        jaClassify.setParentId(id);
        List<JaClassify> list = jaClassifyService.selectJaClassifyList(jaClassify);
        return success(list);
    }


    // @RequiresPermissions("competition:classify:queryTree")
    @GetMapping("/queryTree")
    public TableDataInfo queryTree(JaClassify jaClassify) {
        startPage();
        List<JaClassify> list = jaClassifyService.queryTree(jaClassify);
        return getDataTable(list);
    }


    //@RequiresPermissions("competition:classify:queryModelTree")
    @GetMapping("/queryModelTree")
    public AjaxResult queryModelTree(JaClassify jaClassify) {
        List<JaClassify> list = jaClassifyService.queryModelTree(jaClassify);
        return success(list);
    }

    /**
     * 获取分类管理详细信息
     */
    // @RequiresPermissions("competition:classify:selectJaClassifyNameByDateType")
    @GetMapping(value = "/selectJaClassifyNameByDateType")
    public AjaxResult selectJaClassifyNameByDateType(@RequestParam("dataType") String dataType) {
        return success(jaClassifyService.selectJaClassifyNameByDateType(dataType));
    }


    @PostMapping("/queryDataTree")
    public AjaxResult queryDataTree(@RequestBody JaClassify jaClassify) {
        List<JaClassify> list = jaClassifyService.queryDataTree(jaClassify);
        return success(list);
    }


    @PostMapping("/queryDataExcelTree")
    public AjaxResult queryDataExcelTree(@RequestBody JaClassify jaClassify, HttpServletRequest request) {
        ServletContext servletContext = request.getServletContext();
        // "/temp" 是服务器临时目录的虚拟路径，根据实际情况可能会有所不同
        String tempDir = servletContext.getRealPath("/temp");
        jaClassify.setTempDir(tempDir);
        List<JaClassify> list = jaClassifyService.queryDataExcelTree(jaClassify);
        return success(list);
    }
}
